<?php
class Table {

	var $arr = array();
	var $data = array();
	var $title = array();
	var $opt = array();
	var $row = array();
	var $row_page = array();
	var $start;
	var $end;
	var $submodule = array();
	var $tool = array();
	var $row_tool = array();
	var $row_per_page = 50;
	var $page = array();
	var $active_page;
	var $totalwidth = 0;
	var $height = 20;
	var $sortable = false;
	var $table_sort = "";
	var $default_width = 200;

    function Table($arr) { 
        $this->arr = $arr;
    }

	function add_data($data) {
		$this->data = $data;
    }

	private function compile_column(){
		foreach($this->arr as $k => $v){

			$tmp['title'] = ($v['title'])?$v['title']:ucfirst($k);
			$tmp['width'] = ($v['width'])?$v['width']:$this->default_width;
			$tmp['style'] = ($v['style'])?$v['style']:"";
			$tmp['sort'] = ($v['sort'])?$v['sort']:"";
			$tmp['align'] = ($v['align'])?"text-align:".$v['align']:"";

			$tmp['key'] = $k;

			$this->totalwidth += $tmp['width'];

			array_push($this->title, $tmp);
		}

		$this->totalwidth += 175;
	}

	function column_height($h){
		$this->height = $h;
	}

	function sortable(){
		global $sort, $dsfm_module;

		$this->sortable = true;

		if($_SESSION['_dsfm_module'] != $dsfm_module){

			$_SESSION['_dsfm_module'] = $dsfm_module;

			$_SESSION['table_order'] = "";
			$_SESSION['table_sort'] = "";
		}

		if($sort == "" && $_SESSION['table_sort'] == ""){
			return;
		}

		if($sort != ""){
			$_SESSION['table_sort'] = $sort;

			if($_SESSION['table_order'] == "ASC"){
				$_SESSION['table_order'] = "DESC";
			}else{
				$_SESSION['table_order'] = "ASC";
			}

			if($_SESSION['table_order'] == "")
				$_SESSION['table_order'] = "ASC";
		}

		$this->table_sort = end(explode(".", $_SESSION['table_sort']));

		return " ORDER BY ".$_SESSION['table_sort']." ".$_SESSION['table_order'];
	}

	private function compile_data(){

		foreach($this->data as $row => $col){
			foreach($col as $k => $v){

				if(isset($this->arr[$k]['type'])){
					switch ($this->arr[$k]['type']) {
						case "order" :	$v = $row + 1;
							break;

						case "image" : $v = "<a href = '$v' target = '_blank'><img src = '".$v."' class = 'image'></a>";
							break;

						case "datetime" :
							break;
					}
				}

				if($this->arr[$k]['value']){
					if(strstr($this->arr[$k]['value'], "function")){

						$function = trim(str_replace("function", "", $this->arr[$k]['value']));
						$args = preg_split("/[(]/", $function, 2);
						$function_name = $args[0];

						$args[1] = substr(trim($args[1]),0,-1);
						$args = explode(",",$args[1]);

						$var = "";
						for($i = 0; $i < count($args); $i++){
							$args[$i] = trim($args[$i]);
							if($args[$i][0] == '$') {
								$args[$i] = $col[substr($args[$i],1)];
							}
							$var .= $args[$i] . ",";
						}
						$var = substr($var, 0, -1);

						if ( !function_exists('$function_name') ) :
							$v = call_user_func($function_name, $var);
//							eval("\$v = call_user_func_array(\$function_name, array(".$var.") );");
						endif;

					}else{
						$v = $this->arr[$k]['value'];
					}
				}

				if(isset($this->arr[$k]['length'])){
					$v = excerpt($v, $this->arr[$k]['length']);
				}

				if(isset($this->arr[$k]['link'])){
					$link = $this->phase_link($row, $this->arr[$k]['link']);
					$v = "<a href = '".$link."'>".$v."</a>";
				}

				$tmp[$k] = $v;
			}

			array_push($this->row, $tmp);
		}
	}

	function compile_pagenavi(){

		global $p;

		$this->active_page = $p;

        if($p == null) $p = 1;
        elseif($p < 1) $p = 1;
        elseif($p > ceil(count($this->row) / $this->row_per_page))
            $p = ceil(count($this->row) / $this->row_per_page);

        $this->start = ($p-1) * $this->row_per_page;
        $this->end = $start + $this->row_per_page;

		if( ceil(count($this->row)/$this->row_per_page) < 2) return false;

        for($i = 1; $i <= ceil(count($this->row) / $this->row_per_page); $i++){

			$tmp['page'] = $i;

            if($i == $this->active_page){
				$tmp['active'] = "active";
            }else{
				$tmp['active'] = "";
            }

			array_push($this->page, $tmp);
        }

		$this->row = array_splice ( $this->row, $this->start, $this->row_per_page );
    }

    function set_row_per_page($p){
        $this->row_per_page = $p;
    }

	function add_tool($arr){
		$this->tool = $arr;
	}

	function phase_link($row, $link){
		global $dsfm_module;

        $uri = explode('?', $link, 2);
        $args = explode("&", $uri[1]);

        $req = "";

        for($i = 0; $i < count($args); $i++){
            $p = explode("=", $args[$i]);
            if($p[1][0] == '$'){
                $p[1] = substr($p[1], 1);
                $p[1] = $this->data[$row][$p[1]];
            }
            $req .= $p[0]."=".$p[1]."&";
        }
        $req = substr($req, 0, -1);

        return URL."admin/" . $dsfm_module . "/" . $uri[0]."?".$req;
    }

	function add_submodule($arr){
		$arr['link'] = URL ."admin/" . $arr['module']."/". $arr['file'];
		array_push($this->submodule, $arr);
	}

	function compile_tool(){
		foreach($this->tool as $k => $v){

			$tool_tmp = array();

			foreach($this->data as $row => $col){

				$tmp['link'] = $this->phase_link($row, $v['link']);
				$tmp['icon'] = URL . "admin/html/images/" . $v['icon'];
				$tmp['tool'] = $k;

				array_push($tool_tmp, $tmp);
			}

			$tool_tmp = array_splice ( $tool_tmp, $this->start, $this->row_per_page );

			array_push($this->row_tool, $tool_tmp);
		}
	}

	function display(){
		
	}

	function compile(){
		$this->compile_column();
		$this->compile_data();
		$this->compile_pagenavi();
		$this->compile_tool();
	}

	function rain_display($tpl, $template = "table"){

		$this->compile();

		$tpl->assign("totalwidth", $this->totalwidth);
		$tpl->assign("height", $this->height);
		$tpl->assign("table_menu", $this->submodule);
		$tpl->assign("title", $this->title);
		$tpl->assign("row", $this->row);
		$tpl->assign("page", $this->page);
		$tpl->assign("tool", $this->row_tool);
		$tpl->assign("sortable", $this->sortable);
		$tpl->assign("table_order", $_SESSION['table_order']);
		$tpl->assign("table_sort", $this->table_sort);

		$tpl->draw($template);
	}
}
?>